<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>H+ 后台主题UI框架 - 表单验证 jQuery Validation</title>
    <meta name="keywords" content="H+后台主题,后台bootstrap框架,会员中心主题,后台HTML,响应式后台">
    <meta name="description" content="H+是一个完全响应式，基于Bootstrap3最新版本开发的扁平化主题，她采用了主流的左右两栏式布局，使用了Html5+CSS3等现代技术">
    <link rel="shortcut icon" href="favicon.ico"> <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">

    <script th:inline="javascript">
        var consumer = /*[[${consumer}]]*/ null;

    </script>


</head>

<body class="gray-bg">
<div class="container">
<div class="wrapper wrapper-content animated fadeInRight mx-auto">
  <div class="text-center">
     <div class="row">
        <div class="col-sm-6 col-sm-offset-3">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <h2>消费者个人资料</h2>
                    <p>您的消费者账号: <span class="form-control" th:text="${consumer.consumer_id}">Test ID</span></p>
                    <p>您的消费者姓名: <span class="form-control" th:text="${consumer.consumer_name}">Test Name</span></p>
                    <!-- 其他用户信息... -->
                </div>
                <div class="ibox-title">

                    <h5>编辑消费者信息</h5>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="editConsumerForm" action="/backend-api-endpoint/updateConsumer">

                        <div class="form-group">
                            <label class="col-sm-3 control-label">账号：</label>
                            <div class="col-sm-8">
                                <input id="consumer_id" name="consumer_id" type="text" class="form-control" th:value="${consumer != null ? consumer.consumer_id : ''}" readonly>

                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">姓名：</label>
                            <div class="col-sm-8">
                                <input id="consumer_name" name="consumer_name" type="text" class="form-control"
                                       th:value="${consumer.consumer_name}" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">旧密码：</label>
                            <div class="col-sm-8">
                                <input id="old_password" name="old_password" type="password" class="form-control" th:placeholder="旧密码" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">新密码：</label>
                            <div class="col-sm-8">
                                <input id="consumer_password" name="consumer_password" type="password"
                                       class="form-control" th:placeholder="新密码" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-3">
                                <button class="btn btn-primary" type="button" onclick="updateConsumer()">保存</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
  </div>
</div>
</div>

<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>

<!-- 自定义js -->
<script src="js/content.js?v=1.0.0"></script>
<!-- 更新操作的JavaScript -->
<script>
    $(document).ready(function () {
        // 获取用户信息
        $.ajax({
            url: '/backend-api-endpoint/getConsumerById?consumerId=' + consumerId,
            method: 'POST',
            contentType: 'application/json',
            success: function (response) {
                // 更新表单中的数据
                $("#consumer_id").val(response.consumer_id);
                $("#consumer_name").val(response.consumer_name);

                // 在这里赋值consumer对象
                consumer = response;
            },
            error: function (error) {
                console.error('获取消费者信息时出错：', error);
                alert('获取消费者信息时出错，请重试。');
            }
        });
    });

    function updateConsumer() {
        // 获取表单数据
        var consumerData = {
            consumer_id: $("#consumer_id").val(),
            consumer_name: $("#consumer_name").val(),
            old_password: $("#old_password").val(),
            consumer_password: $("#consumer_password").val()
        };

        // 发送数据到后端
        $.ajax({
            url: '/backend-api-endpoint/updateConsumer',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(consumerData),
            success: function (response) {
                // 处理成功响应，可以根据需要刷新页面等操作
                console.log(response);
                alert('消费者信息更新成功！');
                top.location.reload()
            },
            error: function (error) {
                console.error('更新消费者信息时出错：', error.responseText);

                // 根据错误消息显示适当的提示给用户
                if (error.responseText.includes('Old password does not match.')) {
                    alert('旧密码不匹配，请重新输入。');
                } else {
                    alert('更新消费者信息时出错，请重试。');
                }
            }
        });
    }
</script>
</body>

</html>